import org.json.simple.JsonObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private static Socket socket;
    public static boolean connect_state = false;

    public static void main(String[] args) {
        while (!connect_state){
            connect();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
    public static void connect(){
        try {
            socket = new Socket("127.0.0.1",9999);
            connect_state = true;
            if (connect_state) {
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                new Thread(new Client_listen(socket)).start();
                new Thread(new Client_send(socket, oos)).start();
                new Thread(new Client_heart(socket, oos)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
            connect_state = false;
        }
    }
    public static void reconnect(){
        while (!connect_state){
            System.out.println("正在尝试重新连接！！");
            connect();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Client_listen implements Runnable{

    private Socket socket;
    Client_listen(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            while (true){
                System.out.println(ois.readObject());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Client_send implements Runnable{
    private Socket socket;
    private ObjectOutputStream oos;
    Client_send(Socket socket, ObjectOutputStream oos){
        this.socket = socket;
        this.oos = oos;
    }

    @Override
    public void run() {
        try {
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入发送内容：");
                String input = scanner.nextLine();
                JsonObject object = new JsonObject();
                object.put("type","chat");
                object.put("msg",input);
                oos.writeObject(object);
                oos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            try {
                socket.close();
                Client.connect_state = false;
                Client.reconnect();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}

class Client_heart implements Runnable{
    private Socket socket;
    private ObjectOutputStream oos;
    Client_heart(Socket socket, ObjectOutputStream oos){
        this.socket = socket;
        this.oos = oos;
    }
    @Override
    public void run() {
        try {
            System.out.println("心跳包线程已经启动！");
            int i = 0;
            while (true){
                i++;
                Thread.sleep(5000);
                JsonObject object = new JsonObject();
                object.put("type","heart");
                object.put("msg","心跳包" + i);
                oos.writeObject(object);
                oos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                socket.close();
                Client.connect_state = false;
                Client.reconnect();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

        }
    }
}
